package com.example.beson.androiddemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.example.beson.androiddemo.fragment.AndroidFragment;
import com.example.beson.androiddemo.fragment.IosFragment;
import com.example.beson.androiddemo.fragment.JavaFragment;
import com.example.beson.androiddemo.fragment.PhpFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ToolBarAndViewPagerActivity extends AppCompatActivity {

    @BindView(R.id.viewpager_toolbar)
    Toolbar viewpagerToolbar;
    @BindView(R.id.viewpager_coordinator)
    CollapsingToolbarLayout viewpagerCoordinator;
    @BindView(R.id.viewpager_appbar)
    AppBarLayout viewpagerAppbar;
    @BindView(R.id.viewpager_layout)
    ViewPager viewpagerLayout;
    @BindView(R.id.viewpager_floatbutton)
    FloatingActionButton viewpagerFloatbutton;
    @BindView(R.id.viewpager_tab)
    TabLayout viewpagerTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tool_bar_and_view_pager);
        ButterKnife.bind(this);

        setSupportActionBar(viewpagerToolbar);
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.mipmap.choice_icon);
        }
        viewpagerFloatbutton.setVisibility(View.INVISIBLE);
        initTableLayout();
    }
    /**
     * 初始化tablayout
     */
    private void initTableLayout() {
        //设置MODE:滚动 a.MODE_FIXED把宽度均分
        viewpagerTab.setTabMode(TabLayout.MODE_FIXED);
        //设置背景
        viewpagerTab.setBackgroundColor(Color.WHITE);
        //3.初始化ViewPager
        //设置Tab中TextView的颜色
        viewpagerTab.setTabTextColors(Color.parseColor("#666666"),Color.parseColor("#00ff00"));
        //设置滑动块的高度
        viewpagerTab.setSelectedTabIndicatorHeight(5);
        //设置滑动块的颜色
        viewpagerTab.setSelectedTabIndicatorColor(Color.parseColor("#00ff00"));
        //监听滑动到哪个fragment
        viewpagerLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position==3){
                    viewpagerFloatbutton.setVisibility(View.VISIBLE);
                }else{
                    viewpagerFloatbutton.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //联动
        initTabs();
    }
    /**
     * 初始化tabs
     */
    String[] titles;
    List<Fragment> fraglist;
    private void initTabs() {
        titles=new String[]{"Android","IOS","PHP","Java"};
        fraglist=new ArrayList<>();
        fraglist.add(new AndroidFragment());
        fraglist.add(new IosFragment());
        fraglist.add(new PhpFragment());
        fraglist.add(new JavaFragment());
        viewpagerLayout.setAdapter(fragmentpageradater);
        viewpagerTab.setupWithViewPager(viewpagerLayout);
    }
    private FragmentPagerAdapter fragmentpageradater=new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return fraglist.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if(position>=0&&position<titles.length){
                return titles[position];
            }
            return null;
        }

        @Override
        public int getCount() {
            return 4;
        }
    };

    @OnClick(R.id.viewpager_floatbutton)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.viewpager_floatbutton:
                Snackbar.make(view,"show flaotingactionbutton click listner",Snackbar.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                break;
            default:
                break;
        }
        return true;
    }
}
